package com.yf.ccdugtfood.ui.home;

import android.content.Context;
import android.content.Intent;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.yf.ccdugtfood.activity.MainActivity;
import com.yf.ccdugtfood.activity.OneFoodActivity;

import com.yf.ccdugtfood.R;
import com.yf.ccdugtfood.adapter.HomeAdapter;
import com.yf.ccdugtfood.bean.Shop;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HomeFragment extends Fragment implements ViewPagerEx.OnPageChangeListener,BaseSliderView.OnSliderClickListener{
    private ListView home_shop_listView;
    private List<Shop> shopList = new ArrayList<>();
    private SliderLayout mSlider;
    private GridView mGridView;
    private Context mContext;
    private  MyAdapter myAdapter;
    private MainActivity mainActivity;
    BigDecimal offerPrice = new BigDecimal(50);
    BigDecimal distributionCost = new BigDecimal(5);
    //private SearchView searchView;
    private SwipeRefreshLayout swipeRefreshLayout;
    //定义图标数组
    private int[] gvImage = {
            R.mipmap.rice,
            R.mipmap.soup,
            R.mipmap.chicken
    };
    //定义图标下方的名称数组
    private String[] gvName = {
            "荔香园",
            "悦香园",
            "第三食堂"
    };
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home,container,false);
        mSlider = (SliderLayout)view.findViewById(R.id.slider);
        mGridView = (GridView)view.findViewById(R.id.grid_view);
        home_shop_listView = (ListView) view.findViewById(R.id.home_shop_listView);
        mContext = getActivity();
        //initSearchView(view);搜索框
        initGridView();
        initListView();
        return view;
    }

    private void initListView() {
       /* Shop shop1 = new Shop();
        shop1.setId(1);
        shop1.setShopName("蛋糕房");
        shop1.setSaleNum(996);
        shop1.setOfferPrice(offerPrice);
        shop1.setDistributionCost(distributionCost);
        shop1.setWelfare("进店可获得一个香草冰淇淋");
        shop1.setTime("配送约2-5小时");
        shop1.setShopPic(R.mipmap.shop1);
        shop1.setShopNotice("公告：下单后2-5小时送达！请耐心等候");
        shopList.add(shop1);*/

        /*Shop shop2 = new Shop();
        shop2.setId(2);
        shop2.setShopName("爪哇咖啡.西餐.酒吧");
        shop2.setSaleNum(18);
        shop2.setOfferPrice(offerPrice);
        shop2.setDistributionCost(distributionCost);
        shop2.setWelfare("进店即可送一杯拿铁咖啡");
        shop2.setTime("配送约40分钟");
        shop2.setShopPic(R.mipmap.shop2);
        shop2.setShopNotice("公告：本店周一到周五所有套餐打八折，送咖啡。");
        shopList.add(shop2);*/

        Shop shop3 = new Shop();
        shop3.setId(1);
        shop3.setShopName("必胜客");
        shop3.setSaleNum(99);
        shop3.setOfferPrice(offerPrice);
        shop3.setDistributionCost(distributionCost);
        shop3.setWelfare("下单即可获得一个￥5优惠券");
        shop3.setTime("配送约20分钟");
        shop3.setShopPic(R.mipmap.shop3);
        shop3.setShopNotice("公告：狂欢尽兴 必胜有礼 5折开抢。");
        shopList.add(shop3);

        Shop shop4 = new Shop();
        shop4.setId(2);
        shop4.setShopName("艾尚夜宵");
        shop4.setSaleNum(99);
        shop4.setOfferPrice(offerPrice);
        shop4.setDistributionCost(distributionCost);
        shop4.setWelfare("下单即可获得一个￥15优惠券");
        shop4.setTime("配送约45分钟");
        shop4.setShopPic(R.mipmap.shop4);
        shop4.setShopNotice("公告：本店赠送爱心早餐。");
        shopList.add(shop4);

        Shop shop5 = new Shop();
        shop5.setId(3);
        shop5.setShopName("上岛咖啡");
        shop5.setSaleNum(299);
        shop5.setOfferPrice(offerPrice);
        shop5.setDistributionCost(distributionCost);
        shop5.setWelfare("下单即可获得一个￥20优惠券");
        shop5.setTime("配送约30分钟");
        shop5.setShopPic(R.mipmap.shop5);
        shop5.setShopNotice("公告：本店牛排买一送一。");
        shopList.add(shop5);

        HomeAdapter homeAdapter = new HomeAdapter(mContext,shopList);
        home_shop_listView.setAdapter(homeAdapter);
    }
    /*private void initSearchView(View view) {
        searchView = view.findViewById(R.id.searchView);
        searchView.setQueryHint("请输入菜品名称");
        searchView.setIconifiedByDefault(false);
    }*/

    private void initGridView() {
        HashMap<String,Integer> hashMap = new HashMap<String,Integer>();
        hashMap.put("美好的大学时光",R.mipmap.two);
        hashMap.put("欢迎回家",R.mipmap.one);
        hashMap.put("总是容易流逝",R.mipmap.three);
        hashMap.put("阔别多日，你是否也想它了",R.mipmap.four);

        for(String hm : hashMap.keySet()){
            TextSliderView textSliderView = new TextSliderView(mContext);
            textSliderView
                    .description(hm)
                    .image(hashMap.get(hm))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener( this);
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle().putString("extra",hm);
            mSlider.addSlider(textSliderView);
        }
        mSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mSlider.setCustomAnimation(new DescriptionAnimation());
        mSlider.setDuration(4000);
        mSlider.addOnPageChangeListener(this);
        myAdapter = new MyAdapter();
        mGridView.setAdapter(myAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        Intent intent = new Intent(mContext, OneFoodActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Toast.makeText(mContext, "敬请期待...", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(mContext, "规划中...", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });


    }
    @Override
    public void onSliderClick(BaseSliderView slider) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return gvName.length;
        }

        @Override
        public Object getItem(int position) {
            return gvName[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(mContext,R.layout.item_gridview,null);
            TextView tv_title = view.findViewById(R.id.tv_item);
            ImageView iv_icon = view.findViewById(R.id.iv_item);
            tv_title.setText(gvName[position]);
            iv_icon.setBackgroundResource(gvImage[position]);
            return view;
        }
    }
}
